package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.EmployeeIndicatorScore;
import com.ruoyi.system.domain.dto.EmployeeAssessDetailVO.Indicators;
import com.ruoyi.system.domain.dto.EmployeeAssessDetailVO.IndicatorsType;
import com.ruoyi.system.domain.dto.EmployeeAssessDetailVO.Top;
import com.ruoyi.system.domain.vo.EmployeeAssessDetailVO;
import com.ruoyi.system.mapper.AssessTypeMapper;
import com.ruoyi.system.mapper.EmployeeAssessmentDetailMapper;
import com.ruoyi.system.mapper.EmployeeIndicatorScoreMapper;
import com.ruoyi.system.service.IEmployeeAssessmentDetailService;
import com.ruoyi.system.utils.IndicatorsClassifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeAssessmentDetailImpl implements IEmployeeAssessmentDetailService {

    @Autowired
    private EmployeeAssessmentDetailMapper employeeAssessmentDetailMapper;

    @Autowired
    private AssessTypeMapper assessTypeMapper;
    @Autowired
    private EmployeeIndicatorScoreMapper employeeIndicatorScoreMapper;

    @Override
    public EmployeeAssessDetailVO getEmployeeAssessDetailVO(int employeeId) {
        Top top = employeeAssessmentDetailMapper.selectTop(employeeId);
        Double finalScore = employeeAssessmentDetailMapper.selectFinalScore(employeeId);
        List<Indicators> indicators = employeeAssessmentDetailMapper.selectIndicators(employeeId);

        List<IndicatorsType> indicatorsTypes = IndicatorsClassifier.classifyIndicators(indicators,assessTypeMapper);

        EmployeeAssessDetailVO employeeAssessDetailVO = new EmployeeAssessDetailVO();
        employeeAssessDetailVO.setEmployeeAssessDetailId(top.getEmployeeAssessDetailId());
        employeeAssessDetailVO.setAssessmentStatus(top.getAssessStatus());
        employeeAssessDetailVO.setPlanName(top.getPlanName());
        employeeAssessDetailVO.setCycle(top.getCycle());
        employeeAssessDetailVO.setFinalScore(finalScore);
        employeeAssessDetailVO.setTypes(indicatorsTypes);

        return employeeAssessDetailVO;
    }

    @Override
    public int addEmployeeAssessmentDetail(int employeeId, int indicatorsId, String score) {
        EmployeeIndicatorScore  employeeIndicatorScore = new EmployeeIndicatorScore();
        employeeIndicatorScore.setEmployeeId(employeeId);
        employeeIndicatorScore.setIndicatorId(indicatorsId);
        employeeIndicatorScore.setScore(score);
        return employeeIndicatorScoreMapper.updateScoreByEmployeeIdAndIndicatorsId(employeeIndicatorScore);
    }

    @Override
    public int updateEmployeeAssessmentDetail(int employeeId, String status) {
        return employeeAssessmentDetailMapper.updateStatusByEmployeeId(employeeId,status);
    }

    @Override
    public int updateEmployeeAssessmentDetailFinalScore(int employeeId, Double finalScore) {
        return employeeAssessmentDetailMapper.updateFinalScoreByEmployeeId(employeeId,finalScore);
    }
}
